package com.wswy.wzcx.ui.car

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.content.Context
import com.che.libcommon.utils.JsonKit
import com.che.libcommon.utils.ToastUtils
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.model.*
import com.wswy.wzcx.model.car.CarTypeData
import com.wswy.wzcx.model.license.UserCarInfo
import com.wswy.wzcx.model.resp.QueryResult
import com.wswy.wzcx.module.UserDataProvider
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.module.base.onResult
import com.wswy.wzcx.utils.Tools

class AddCarInfoViewModel : AddCarPreViewModel(){

    val carInfoModel = CarInfoModel()

    val carTypeData=CarTypeData()

    private val addCarLiveData=MutableLiveData<Resource<QueryResult?>>()

    private val deleteLiveData = MutableLiveData<Resource<Any?>>()

    private val updateLiveData = MutableLiveData<Resource<UserCarInfo?>>()

    private val subscribeNotifyLiveData = MutableLiveData<Resource<Any?>>()

    var sendOcrResult : List<OCRItem>?=null

    var subscribeNotify:Boolean = Tools.getSp().getBoolean(DataCenter.KEY_SUBSCRIBE_NOTIFY,false)
    set(value) {
        if (changeSubscribeNotify(value)){
            field = value
        }
    }

    private var startLogin = false

    override fun onCreate(){
        super.onCreate()
        registerObservers()
    }




    fun updateEdit(chooseCarData: CarTypeData){
        carTypeData.brand = chooseCarData.brand
        carTypeData.series = chooseCarData.series
        carTypeData.model = chooseCarData.model


        carInfoModel.seriesId = chooseCarData.series?.id
        carInfoModel.specificationId = chooseCarData.model?.id
    }

    override fun onUserLoginSuccess(userMode: UserMode?) {
        super.onUserLoginSuccess(userMode)
        if (startLogin){
            doAddCar()
        }
    }


    fun addCar(context: Context){
        if (!hasLogin()){
            gotoLogin(context)
            startLogin = true
            return
        }

        doAddCar()

    }

    private fun changeSubscribeNotify(status:Boolean):Boolean{
        if (subscribeNotifyLiveData.value?.inLoading() == true){
            ToastUtils.showText("操作太快了~")
            return false
        }
        //已登录
        if (hasLogin()){
            addRun(Api.get().subscribeNotify("violation",status).onResult(subscribeNotifyLiveData))
        }

        return true
    }

    private fun doAddCar(){
        addCarLiveData.postValue(Resource.loading())

        addRun(UserDataProvider.get().violationQuery(carInfoModel.convertQueryMap()).onResult({resp ->

            resp?.run {
                addCarLiveData.postValue(Resource.success(Converter.convert2Result(this)))
            }


        },{
            addCarLiveData.postValue(Resource.error(it))
        }))


        reportOCR()
    }

    private fun reportOCR(){
        //上报ocr结果
        sendOcrResult?.run {
            Api.get().reportOcrResult(JsonKit.GSON.toJson(this)).onResult({
                sendOcrResult = null
            },{
                sendOcrResult = null
            })
        }

    }

    fun deleteCar(carId:Int){
        addRun(UserDataProvider.get().deleteCar(carId).onResult(deleteLiveData))
    }

    fun updateCar(carId:Int){
        addRun(UserDataProvider.get().updateCar(carId,carInfoModel.convertQueryMap()).onResult(updateLiveData))
    }

    fun getAddCarLiveData():LiveData<Resource<QueryResult?>> = addCarLiveData

    fun getDeleteLiveData():LiveData<Resource<Any?>> = deleteLiveData

    fun getUpdateLiveData():LiveData<Resource<UserCarInfo?>> = updateLiveData


    fun getSubscribeNotifyLiveData():LiveData<Resource<Any?>> = subscribeNotifyLiveData
}